/**
 * author:Administrator
 * date:2015/7/27
 * desc:TODO
 */

'use strict';

var Router = require('koa-router');
var config = require('../../../config/config.js');
let daoDispatcher = require('../../../dao/role.js');

console.log('router:' + config.baseUrl + '/v2/role');
var router = new Router({
    prefix: config.baseUrl + '/v2/role'
});

module.exports = router.routes();

/**
 * 在权限系统中, 添加一个角色,并设置权限(如果该角色已存在,则覆盖权限)
 * post /api/v2/role/
 {
     "roleName": "管理员",
     "permission":{
        "商家管理": ["添加商家","编辑商家"],
        "商家详情": ["商家详情"]
     }
 }
 */
router.post('/', function*() {
    let result = yield daoDispatcher.add(this.request.inParams);
    this.body = result;
});

/**
 * 获取某个角色的权限图
 * get /api/v2/role/permissions?roleName=管理员
 */
router.get('/permissions', function*() {
    let result = yield daoDispatcher.permissions(this.request.inParams);
    this.body = result;
});

/**
 * 查询某个角色是否有 指定的单个权限
 * post /o2o/api/role/v2/check
 {
  "roleName": "管理员",
  "groupName": "商家管理",
  "permissionName": "添加商家",
  "act":1
  }
 */
router.post('/check', function*() {
    let result = yield daoDispatcher.checkPermission(this.request.inParams);
    this.body = result;
});
